<template>
	<view class="u-p-l-20 u-p-r-20">
		<u-cell-group>
			<u-cell-item icon="setting" icon-size='40' :title="title" @click="selectMenu">
			</u-cell-item>
			<u-cell-item v-if="title!='到店自提'" icon="map" icon-size='40' :title="address.name?address.name+' '+address.tel:'请选择地址信息'" @click="$u.route('/pages/shop/address/address',{type:'select'})">
			</u-cell-item>
		</u-cell-group>
		<view class="u-m-t-20" v-for="(item,index) in payList" :key="index">
			<view class="u-flex u-flex-1">
				<u-image width="160rpx" height="160rpx" :show-loading='false' :src="item.src"></u-image>
				<view class="u-m-l-60 u-flex-1">
					<view class="">
						{{item.title}}
					</view>
					<view class="u-font-22 u-m-t-10 u-m-b-10">
						{{item.goods.remarks}}
					</view>
					<view class="u-font-22 u-m-t-10 u-m-b-10" v-if="item.goods.useScore==0" style="color: red;">
						不允许使用积分购买
					</view>
					<view class="u-flex u-row-between" style="color: red;">
						<text>￥{{item.price | getPrice}}</text>
						<text>x{{item.count}}</text>
					</view>
				</view>
			</view>
		</view>
		<view class="u-flex u-row-between u-m-t-20">
			<text>合计：</text>
			<text>￥{{price | getPrice}}</text>
		</view>
		<view class="" v-if="userInfo.score">
			<view class="u-flex u-row-between u-m-t-20">
				<text>使用积分：</text>
				<u-number-box :min="0" :max='maxScore' v-model="value" @change="valChange"></u-number-box>
			</view>
			<view class="u-font-22" style="color: red;">
				1积分可抵￥{{proportion | getPrice}}元，可用积分{{userInfo.score}}
			</view>
			<view class="u-font-22 u-m-t-10 u-tips-color">
				注:积分抵扣规则以当前页面为准
			</view>
		</view>
		<view class="" style="height: 150rpx;">
			
		</view>
		<view class="u-p-20 u-flex u-row-between w-pay-btn">
			<text class="u-flex-1" style="color: red;">实付：￥{{(price-proportion*value)|getPrice}}</text>
			<u-button type="error" @click="submit" shape="circle">提交订单</u-button>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	import {
		getScoreRule,orderSave,startPay,paySuccess
	} from '@/api/shop.js'
	import {
		getByMobile
	} from '@/api/index.js'
	export default {
		computed: {
			...mapState(['payList', 'userInfo','address']),
			price() {
				let count = 0
				this.payList.forEach(item => {
					count += item.price*item.count
				})
				return count
			},
			maxScore(){
				let count = 0
				this.payList.forEach(item => {
					if(item.goods.useScore){
						count += item.price*item.count
					}
				})
				const s = Math.floor(count/this.proportion)
				const m = this.userInfo.score > s?s:this.userInfo.score
				return m
			}
		},
		data() {
			return {
				value: 0,
				data: [{
						id: 0,
						name: "到店自提"
					},
					{
						id: 1,
						name: "物业配送"
					},
					{
						id: 2,
						name: "快递物流"
					},
				],
				title: '到店自提',
				shippingType:0,
				proportion: 0,
				idAddress:""
			}
		},
		onLoad() {
			this.getScoreRule()
			this.getUserInfo()
		},
		methods: {
			submit(){
				const idCarts = this.payList.map(item => {
					return item.id
				})
				if(this.title=='到店自提'){
					this.idAddress = ''
				}else{
					if(!this.address.id){
						this.$u.toast('请选择地址')
						return
					}
					this.idAddress = this.address.id
				}
				orderSave({
					JSESSIONID: uni.getStorageSync('sessionid'),
					idUser:this.userInfo.id,
					idCarts,
					idAddress:this.idAddress,
					shippingType:this.shippingType,
					score:this.value,
					proportion:this.proportion
				}).then(res=>{
					const total = this.price-this.proportion*this.value
					if(total){
						this.$u.route({
							url:'/pages/shop/pay/pay',
							params:{totalPrice:total,orderSn:res.orderSn},
							type:'redirectTo'
						})
					}else{
						startPay({JSESSIONID: uni.getStorageSync('sessionid'),orderSn:res.orderSn}).then(res=>{
							paySuccess({JSESSIONID: uni.getStorageSync('sessionid'),orderSn:res.orderSn,payId:res.payId}).then(res=>{
								this.$u.route({
									url:'/pages/shop/order/detail/detail',
									params:{orderSn:res.orderSn},
									type:'redirectTo'
								})
							})
						})
					}
					this.$store.dispatch('cartQueryByUser')
				})
			},
			getUserInfo() {
				getByMobile({
					mobile: this.userInfo.mobile
				}).then(res => {
					this.$store.commit('setUserInfo', res)
				})
			},
			getScoreRule() {
				getScoreRule().then(res => {
					console.log(res);
					this.proportion = res.proportion
				})
			},
			valChange(e) {
				this.value = e.value
			},
			selectMenu() {
				uni.showActionSheet({
					itemList: ['到店自提', '物业配送', '快递物流'],
					success: (res) => {
						this.title = this.data[res.tapIndex].name
						this.shippingType = this.data[res.tapIndex].id
					},
					fail: function(res) {
						console.log(res.errMsg);
					}
				});
			}
		}
	}
</script>

<style scoped>
.w-pay-btn{
	position: fixed;
	width: 100%;
	left: 0;
	bottom: 0;
	background-color: #FFFFFF;
}
</style>
